<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>修改角色信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>
    <script type="text/javascript" src="/js/cookie.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<style>
</style>
<body>
<div class="x-body">
    <form class="layui-form" id="update">
        <input type="hidden" name="userId" id="userId" />
        <div class="layui-form-item">
            <label for="addRole" class="layui-form-label">
               添加角色
            </label>
                <div class="layui-input-inline" id="addRole" lay-filter = "addRole">
                </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-inline" style="text-align: center;">
                <button type="button" class="layui-btn" lay-filter="edit" lay-submit>
                    确定
                </button>
            </div>
        </div>

    </form>
</div>
<script>
    layui.use(['form', 'layer'], function () {
        var $ = layui.jquery;
        $(function ($) {
            var form = layui.form
                , layer = layui.layer;
            // 获取父窗口中存储的下拉框选中的值
            var checkBox = $("#addRole");
            //获取用户的id
            var userId = parent.$("#userId").val();
            //统计当前用户的所有权限
            var allRole = [];
            //获得当前用户应有的权限
            $.ajax({
                url:"/system/checkRole",
                type:"POST",
                data:{userId:userId},
                success:function (res) {
                    var userRole = res.data;
                    //获取到所有角色列表
                    $.ajax({
                        url:'/role/list',
                        type:'GET',
                        success:function (res2) {
                            var roles = res2.data;
                            $.each(roles,function (index,ele) {
                                var roleCheck = $("<input type='checkbox' name='role'/>");
                                roleCheck.attr("title",ele["roleName"]);
                                roleCheck.attr("roleid",ele["roleId"]);
                                var roleId = ele["roleId"];
                                //获取用户本有的角色，并选中
                                $.each(userRole,function (index,elem) {
                                   if (roleId==elem){
                                       roleCheck.prop("checked",true);
                                       allRole.push(elem);
                                   }
                                });
                                checkBox.append(roleCheck);
                                form.render();
                            });
                        }
                    });

                }
            })
            form.on('checkbox', function(data){
                console.log(data.elem); //得到checkbox原始DOM对象
                console.log(data.elem.checked); //是否被选中，true或者false
                var check = data.elem;
                var roleNum = $(check).attr("roleid");
                if (check.checked) {
                    allRole.push(parseInt(roleNum));
                    console.log(allRole);
                }
                else {
                    var index = allRole.indexOf(roleNum);
                    allRole.splice(index,1);
                    console.log(allRole);
                }
            });

            //监听提交
            form.on('submit(edit)', function (data) {
                var postData = {
                    'userId':parseInt(userId),
                    'roleIds':allRole
                };
                //发异步，把数据提交给后台
                $.ajax({
                    url: '/userRole/edit',
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(postData),
                    success: function (res) {
                        if (res.status == 0) {
                            layer.alert("修改成功", {icon: 6}, function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                                parent.location.reload();
                            });
                        } else {
                            layer.alert("修改失败", {icon: 5})
                        }
                    },
                })
                return false;

            });

        });

    });
</script>

</body>

</html>